package com.ruoyi.web.controller.danger;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.opio.util.ExampleBase;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.SysDangerPoint;
import com.ruoyi.system.entity.SysProductionPoint;
import com.ruoyi.system.service.SysProductionPointService;
import com.ruoyi.system.service.impl.SysProductionPointServiceImpl;
import freemarker.template.TemplateException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生产实时点位(SysProductionPoint)表控制层
 *
 * @author makejava
 * @since 2023-06-01 14:03:46
 */
@RestController
@RequestMapping("sysProductionPoint")
public class SysProductionPointController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private SysProductionPointService sysProductionPointService;

    @Resource
    private SysProductionPointServiceImpl sysProductionPointServiceimpl;
    /**
     * 分页查询
     *
     * @param sysProductionPoint 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public TableDataInfo queryByPage(SysProductionPoint sysProductionPoint) {
        startPage();
        List<SysProductionPoint> sysProductionPoints = this.sysProductionPointService.queryByPage(sysProductionPoint);
        return getDataTable(sysProductionPoints);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<SysProductionPoint> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.sysProductionPointService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysProductionPoint 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<SysProductionPoint> add(@RequestBody SysProductionPoint sysProductionPoint) {
        return ResponseEntity.ok(this.sysProductionPointService.insert(sysProductionPoint));
    }

    /**
     * 编辑数据
     *
     * @param sysProductionPoint 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<SysProductionPoint> edit(@RequestBody SysProductionPoint sysProductionPoint) {
        return ResponseEntity.ok(this.sysProductionPointService.update(sysProductionPoint));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.sysProductionPointService.deleteById(id));
    }

    /**
     * 与实时数据库关联
     * @return
     */
    @GetMapping("/getTimeData")
    public TableDataInfo getTimeData(SysProductionPoint query) {
        startPage();
        List<SysProductionPoint> sysProductionPoints = this.sysProductionPointService.queryByPage(query);
        Map<String, Object> map = ExampleBase.toResult();
        if (CollUtil.isNotEmpty(map)) {
            sysProductionPoints.forEach(sysProductionPoint -> {
                if (map.containsKey(sysProductionPoint.getDatabaseBitNumber())) {
                    Object o = map.get(sysProductionPoint.getDatabaseBitNumber());
                    sysProductionPoint.setAv(o.toString());
                }
            });
        }
        return getDataTable(sysProductionPoints);
    }
    /**
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "生产实时点位(", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<SysProductionPoint> util = new ExcelUtil<>(SysProductionPoint.class);
        List<SysProductionPoint> sysProductionPoints = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = sysProductionPointService.importExcel(sysProductionPoints, updateSupport, operName);
        return success(message);
    }

    /**
     *
     * @param response
     */
    @Log(title = "生产实时点位(", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProductionPoint sysProductionPoint)
    {
        List<SysProductionPoint> sysProductionPoints = this.sysProductionPointService.queryByPage(sysProductionPoint);
        ExcelUtil<SysProductionPoint> util = new ExcelUtil<>(SysProductionPoint.class);
        util.exportExcel(response, sysProductionPoints, "重大危险源点位(");
    }

    /**
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysProductionPoint> util = new ExcelUtil<>(SysProductionPoint.class);
        util.importTemplateExcel(response,"重大危险源点位(");
    }
    /**
     *
     * @return
     * @throws TemplateException
     */
    @GetMapping("/createFile")
    public AjaxResult createFile() throws TemplateException, IOException {
        sysProductionPointServiceimpl.createDroolsFile();
        return AjaxResult.success();
    }
}

